package com.mcxx.modules.system.controller;

import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.system.entity.PolicyEntity;
import com.mcxx.modules.system.service.read.PolicyReadService;
import com.mcxx.modules.system.service.write.PolicyWriteService;
import com.mcxx.modules.system.dto.PolicyDTO;
import com.mcxx.modules.system.param.PolicyParam;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Arrays;
import java.util.Date;



@Validated
@Api("数据管理/文件管理")
@RestService({"policyService/v2"})
public class PolicyController extends DefaultRestService {

    @Autowired
    private PolicyReadService commonPolicyReadService;
    @Autowired
    private PolicyWriteService commonPolicyWriteService;
    @Autowired
    private FileService fileService;

    @PostMapping("findList")
    @ApiOperation(value="文件列表页", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="文件列表页")
    public void findList(PolicyParam param, PageParam pageParam){
        Page page = commonPolicyReadService.findPage(param,pageParam);
        this.setPageBean(page);
    }

    @PostMapping("deletePolicy")
    @ApiOperation(value="删除政策文件", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="删除政策文件")
    public void deletePolicy(String id){
        BaseResult<Boolean> rstMsg = new BaseResult<>();
        try {
            rstMsg = commonPolicyWriteService.deletePolicy(Arrays.asList(id.split(",")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.setData("msg", rstMsg.getMsg());
    }


    @PostMapping("savePolicy")
    @ApiOperation(value="保存政策文件", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="删除政策文件")
    public void savePolicy(@RequestBody PolicyDTO policy){
        BaseResult<Boolean> rstMsg = new BaseResult<>();
        try {
            policy.setOrgId(getCurUserAccount().getOrgId());
            policy.setCreateTime(new Date());
            rstMsg = commonPolicyWriteService.addPolicy(policy, policy.getFileList(), getCurUserAccount());
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.setData("msg", rstMsg.getMsg());
    }

    @PostMapping("updatePolicy")
    @ApiOperation(value="更新政策文件", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="更新政策文件")
    public void updatePolicy(@RequestBody PolicyDTO policy){
        BaseResult<Boolean> rstMsg = new BaseResult<>();
        try {
            policy.setOrgId(getCurUserAccount().getOrgId());
            rstMsg = commonPolicyWriteService.updatePolicy(policy, policy.getFileList());
        } catch (Exception e) {
            this.setError(e.getMessage());
        }
        this.setData("msg", rstMsg.getMsg());
    }

    @PostMapping("deletePolicyFile")
    @ApiOperation(value="删除政策文件对应附件", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="删除政策文件对应附件")
    public void deletePolicyFile(String policyFileId){
        BaseResult<Boolean> rstMsg = new BaseResult<>();
        try {
            rstMsg = commonPolicyWriteService.deletePolicyFile(policyFileId);
        } catch (Exception e) {
            this.setError(e.getMessage());
        }
        this.setData("msg", rstMsg.getMsg());
    }


    @PostMapping("viewPolicyDetail")
    @ApiOperation("查看政策文件详情")
    public void viewPolicyDetail(String id){
        PolicyEntity entity = commonPolicyReadService.getById(id);
        if(null != entity){
            String imgUrl = fileService.getFileUrl(entity.getImgUrl());
            entity.setImgUrl(imgUrl);
        }
        setData(entity);
    }

}
